
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>修改头像</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/resources/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/resources/css/public.css" media="all">
    <style>
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div class="layui-form  layuimini-form">
            <div class="layui-form-item">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="uploadImg">上传图片</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" width="100" height="100" border="1" id="userImg">
                        <p id="uploadText"></p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="/resources/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="/resources/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'jquery', 'layer', 'upload'], function () {
        var form = layui.form,
            layer = layui.layer,
            upload = layui.upload,
            $ = layui.jquery;
        let  path='?'
        //图片上传
        let uploadInst = upload.render({
            elem: '#uploadImg'
            , url: '/file/uploadImg.do' //文件上传的地址
            , accept: "images" //指定允许上传时校验的文件类型，可选值有：images（图片）、file（所有文件）、video（视频）、audio（音频） 默认为images
            , acceptMime: 'image/*'
            , field: 'mf'//提交表单域的名字  这里field值为上面请求方法的形参名
            //下面为上传前的操作
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#userImg').attr('src', result); //图片链接（base64）
                });
                layer.msg('上传中', {icon: 16, time: 0});
            }
            , done: function (res) {
                //如果上传失败
                if (res.code > 0) {
                    return layer.msg('上传失败');
                }
                layer.msg(res.msg);

                //上传成功的一些操作
                //更新index.jsp的src的地址、
                path=path+'?'
                //我的头像更网络有很大问题
                window.confirm(res.data+"")
                window.parent.document.getElementById("index_img_title").setAttribute("src", res.data);
                //上面的p标签内容
                $('#uploadText').html(''); //置空上传失败的状态
            }
            , error: function () {
                //演示失败状态，并实现重传
                //上面的p标签
                let uploadText = $('#uploadText');
                //上面的p标签内容
                uploadText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs upload-reload">重试</a>');
                uploadText.find('.upload-reload').on('click', function () {
                    uploadInst.upload();//重新上传
                });
            }
        });
    });
</script>
</body>
</html>
